package io.github.yidasanqian.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import io.github.yidasanqian.IdGenerator;
import io.github.yidasanqian.bean.DozerBeanMappingBuilder;
import io.github.yidasanqian.domain.Goods;
import io.github.yidasanqian.domain.GoodsCategory;
import io.github.yidasanqian.domain.GoodsDescription;
import io.github.yidasanqian.dto.GoodsCategoryDTO;
import io.github.yidasanqian.dto.GoodsDTO;
import io.github.yidasanqian.mapper.GoodsCategoryMapper;
import io.github.yidasanqian.mapper.GoodsDescriptionMapper;
import io.github.yidasanqian.mapper.GoodsMapper;
import io.github.yidasanqian.service.GoodsService;
import io.github.yidasanqian.vo.GoodsCategoryVO;
import io.github.yidasanqian.vo.GoodsVO;
import org.dozer.Mapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author Linyu Chen
 */
@Transactional(rollbackFor = Exception.class)
@Service
public class GoodsServiceImpl implements GoodsService {
    @Resource
    private GoodsMapper goodsMapper;

    @Resource
    private GoodsDescriptionMapper goodsDescriptionMapper;

    @Resource
    private GoodsCategoryMapper goodsCategoryMapper;

    @Override
    public int addGoods(Goods goods, String description) throws Exception {
        Date date = new Date();
        long goodsId = IdGenerator.genGoodsId();
        goods.setId(goodsId);
        goods.setStatus((byte) 1);
        goods.setCreated(date);
        goods.setUpdated(date);
        goodsMapper.insert(goods);

        int row = addGoodsDescription(goodsId, description, date);
        if (row > 0) {
            return 1;
        } else {
            return 0;
        }
    }

    private int addGoodsDescription(long goodsId, String description, Date date) {
        GoodsDescription goodsDescription = new GoodsDescription();
        goodsDescription.setGoodsId(goodsId);
        goodsDescription.setGoodsDesc(description);
        goodsDescription.setCreated(date);
        goodsDescription.setUpdated(date);
        return goodsDescriptionMapper.insert(goodsDescription);
    }

    @Override
    public Goods findGoodsById(long goodsId) {
        return goodsMapper.selectByPrimaryKey(goodsId);
    }

    @Override
    public GoodsDTO findAll(int page, int rows) {
        // 分页查询
        PageHelper.startPage(page, rows);
        List<Goods> goodsList = goodsMapper.selectAll();
        PageInfo<Goods> pageInfo = new PageInfo<Goods>(goodsList);

        Mapper mapper = new DozerBeanMappingBuilder(Goods.class, GoodsVO.class).getDozerMapper();
        List<GoodsVO> goodsVOList = new ArrayList<>();
        for (Goods goods : goodsList) {
            GoodsVO goodsVO = mapper.map(goods, GoodsVO.class);
            goodsVOList.add(goodsVO);
        }

        GoodsDTO goodsDTO = new GoodsDTO();
        goodsDTO.setGoodsVOList(goodsVOList);
        goodsDTO.setTotal(pageInfo.getTotal());
        return goodsDTO;
    }

    @Override
    public GoodsCategoryDTO queryGoodsCategoryNodeList(Long parentId) {
        List<GoodsCategory> goodsCategoryList = goodsCategoryMapper.queryGoodsCategoryNodeList(parentId);

        List<GoodsCategoryVO> goodsCategoryVOList = new ArrayList<>();
        for (GoodsCategory goodsCategory : goodsCategoryList) {
            GoodsCategoryVO categoryVO = new GoodsCategoryVO();
            categoryVO.setId(goodsCategory.getId());
            categoryVO.setText(goodsCategory.getName());
            categoryVO.setIsParent(goodsCategory.getIsParent());
            goodsCategoryVOList.add(categoryVO);
        }
        GoodsCategoryDTO goodsCategoryDTO = new GoodsCategoryDTO();
        goodsCategoryDTO.setGoodsCategoryVOList(goodsCategoryVOList);
        return goodsCategoryDTO;
    }
}
